<?php
	/**
	* 
	*/
	class EnvAction extends Action
	{
		
		public function index(){
			$condition=[];
			if(isset($_REQUEST['search_content']) && !empty($_REQUEST['search_content'])){
				$condition['mainhost']=array('EQ',$_REQUEST['search_content']);
				$condition['branchhost']=array('EQ',$_REQUEST['search_content']);
				$condition['inuser']=array('EQ',$_REQUEST['search_content']);
				$condition['owngroup']=array('LIKE','%'.$_REQUEST['search_content'].'%');
				$condition['_logic'] = 'OR';
			}

			$size=10;
			// 总条数
			$total_counts=D('Env')->getCounts($condition);
			//总页数
			$total_pages=ceil($total_counts/$size);
			$current_p=isset($_REQUEST['p'])?$_REQUEST['p']:1;
			$start=($current_p-1)*$size;
			$previous_p=1;
			$previous_disable=true;
			if($current_p>1){
				$previous_p=$current_p-1;
				$previous_disable=false;
			}
			$next_p=$total_pages;
			$next_disable=true;
			if($current_p<$total_pages){
				$next_p=$current_p+1;
				$next_disable=false;
			}			

			$envs = D('Env')->getEnvs($condition,$start,$size);
			$this->assign('next_p',$next_p);
			$this->assign('next_disable',$next_disable);
			$this->assign('previous_p',$previous_p);
			$this->assign('previous_disable',$previous_disable);
			$this->assign('current_p',$current_p);
			$this->assign('total_pages',$total_pages);
			$this->assign('total_counts',$total_counts);
			$this->assign('envs',$envs);
			$this->assign('title','环境分配');
			$this->display('Env/index');
		}

		public function ajaxDoAdd(){
			$result=array(
				'status'=>0,
				'data'=>'数据缺失',
				);

			if(empty($_REQUEST['mainhost']) || empty($_REQUEST['branchhost']) || empty($_REQUEST['inuser']) || empty($_REQUEST['owngroup'])){
				 $this->ajaxReturn($result);
			}

			$data['mainhost']=$_REQUEST['mainhost'];
			$data['branchhost']=$_REQUEST['branchhost'];
			$data['inuser']=$_REQUEST['inuser'];
			$data['owngroup']=$_REQUEST['owngroup'];
			$data['type']='';
			$data['hostname']='fp'.substr($_REQUEST['branchhost'],-3);
			$data['status']=1;
			$data['modifytime']=date('y-m-d');

			if(!D('Env')->addEnv($data)){
				$result['status']=0;
				$result['data']='插入失败';
				$this->ajaxReturn($result);
			}

			$result['status']=1;
			$result['data']='插入成功';
			$this->ajaxReturn($result);		

		}

		public function ajaxDoDel(){
			$result=array(
				'status'=>0,
				'data'=>'数据错误'
				);

			if(empty($_REQUEST['id'])){
				$this->ajaxReturn($result);
			}else{
				$condition['id']=$_REQUEST['id'];
			}

			if(!D('Env')->delEnv($condition)){
				$result['status']=0;
				$result['data']='删除失败';
				$this->ajaxReturn($result);	
			}

			$result['status']=1;
			$result['data']='删除成功';
			$this->ajaxReturn($result);
		}
	}